package com.sheng.project.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import java.io.IOException;

/**
 * @author by ls
 * @date 2023/9/15
 */
@Slf4j
public class FtpUtil {

    /**
     * 获取 FTPClient 对象
     *
     * @param ftpHost     FTP 主机服务器
     * @param ftpPort     FTP 端口 默认为 21
     * @param ftpPassword FTP 登录密码
     * @param ftpUserName FTP 登录用户名
     * @return FTPClient
     */
    public static FTPClient getFTPClient(String ftpHost, int ftpPort, String ftpUserName, String ftpPassword) throws IOException {
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(ftpHost, ftpPort);
        ftpClient.login(ftpUserName, ftpPassword);
        if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
            ftpClient.disconnect();
            return null;
        } else {
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setBufferSize(2048);
            ftpClient.setDataTimeout(30000);
            return ftpClient;
        }
    }
}
